import request from "@/utils/request";
import type {
  getUserListApiParams,
  GetUserListApiResponse,
  UserItem,
  GetUserRoleResponse,
} from "./model/userModel";

enum Api {
  getUserList = "/admin/acl/user",
  saveUser = "/admin/acl/user/save",
  updateUser = "/admin/acl/user/update",
  deleteUser = "/admin/acl/user/remove",
  batchRemoveUser = "/admin/acl/user/batchRemove",
  doAssignUser = "/admin/acl/user/doAssign",
  toAssignUser = "/admin/acl/user/toAssign",
}

// 获取管理用户分页列表
export const getUserListApi = ({
  page,
  limit,
  username,
}: getUserListApiParams) => {
  return request.get<any, GetUserListApiResponse>(
    `${Api.getUserList}/${page}/${limit}`,
    {
      params: {
        username,
      },
    }
  );
};


// 添加用户
export const saveUserApi = (data: UserItem) => {
  return request.post<any, null>(Api.saveUser, data);
};

// 修改用户
export const updateUserApi = (data: UserItem) => {
  return request.post<any, null>(Api.updateUser, data);
};

// 删除用户
export const removeUserApi = (id: number) => {
  return request.delete<any, null>(`${Api.deleteUser}/${id}`);
};

// 批量删除用户
export const batchRemoveUserApi = (idList: number[]) => {
  return request.delete(Api.batchRemoveUser, {
    data: idList, //请求体参数
  });
};

//  给用户分配角色

export const doAssignUserApi = (userId:number, roleId:number[]) => {
  return request.post<any, null>(Api.doAssignUser, {},{
    params: {
      userId,
      roleId: roleId.join(",")
    },
  });
};


//根据用户获取角色数据

export const getUserRoleApi = (adminId: number) => {
  return request.get<any, GetUserRoleResponse>(
    `${Api.toAssignUser}/${adminId}`
  );
}; 